package com.mylhyl.zxing.scanner;

import com.google.zxing.Result;
import com.mylhyl.zxing.scanner.camera.CameraManager;
import com.mylhyl.zxing.scanner.common.Scanner;
import ohos.agp.components.surfaceprovider.SurfaceProvider;
import ohos.agp.graphics.SurfaceOps;
import ohos.app.Context;
import ohos.media.image.PixelMap;

import java.io.IOException;


public class CameraSurfaceView extends SurfaceProvider implements SurfaceOps.Callback, ScannerViewHandler.HandleDecodeListener {
    private static final String TAG = CameraSurfaceView.class.getSimpleName();
    private ScannerView mScannerView;
    private boolean hasSurface;
    private CameraManager mCameraManager;
    private ScannerViewHandler mScannerViewHandler;

    private boolean lightMode = false;//闪光灯，默认关闭
    private ScannerOptions mScannerOptions;

    CameraSurfaceView(Context context, ScannerView scannerView) {
        super(context);
        this.mScannerView = scannerView;
        hasSurface = false;
    }

    void onResume(ScannerOptions scannerOptions) {
        this.mScannerOptions = scannerOptions;
        this.mCameraManager = new CameraManager(getContext(), mScannerOptions, mSurfaceOps);
        this.mScannerViewHandler = null;

        SurfaceOps surfaceHolder = getSurfaceOps().get();
        if (hasSurface) {
            initCamera(surfaceHolder);
        } else {
            surfaceHolder.addCallback(this);
        }
    }

    private SurfaceOps mSurfaceOps;

    private void initCamera(SurfaceOps surfaceHolder) {
        if (surfaceHolder == null) {
            throw new IllegalStateException("No SurfaceHolder provided");
        }
        if (mCameraManager.isOpen()) {
            return;
        }
        mSurfaceOps = surfaceHolder;
        try {
            mCameraManager.openDriver(surfaceHolder);
            postLayout();
            mCameraManager.setTorch(lightMode);
            if (mScannerViewHandler == null) {
                mScannerViewHandler = new ScannerViewHandler(mScannerOptions, mCameraManager, this);
            }
        } catch (IOException ioe) {
        } catch (RuntimeException e) {

        }
    }

    void onPause() {
        if (mScannerViewHandler != null) {
            mScannerViewHandler.quitSynchronously();
            mScannerViewHandler = null;
        }
        mCameraManager.closeDriver();
    }

    void setTorch(boolean mode) {
        this.lightMode = mode;
        if (mCameraManager != null) mCameraManager.setTorch(lightMode);
    }

    void restartPreviewAfterDelay(long delayMS) {
        if (mScannerViewHandler != null)
            mScannerViewHandler.sendEvent(Scanner.RESTART_PREVIEW, delayMS);
    }

    @Override
    public void surfaceCreated(SurfaceOps surfaceOps) {

        if (!hasSurface) {
            hasSurface = true;
            initCamera(surfaceOps);
        }
    }

    @Override
    public void surfaceChanged(SurfaceOps surfaceOps, int i, int i1, int i2) {

    }

    @Override
    public void surfaceDestroyed(SurfaceOps surfaceOps) {
        hasSurface = false;
        if (!hasSurface && surfaceOps != null) {
            surfaceOps.removeCallback(this);
        }
    }

    CameraManager getCameraManager() {
        return mCameraManager;
    }

    @Override
    public void restartPreview() {
        mScannerView.drawViewfinder();
    }

    @Override
    public void decodeSucceeded(Result rawResult, PixelMap barcode, float scaleFactor) {
        mScannerView.handleDecode(rawResult, barcode, scaleFactor);
    }
}
